package com.sgzn.sgpay.web.controller;

import com.sgzn.sgpay.enums.SgsResponseCode;
import com.sgzn.sgpay.model.request.SgsShopIncomePreInfoRequest;
import com.sgzn.sgpay.model.request.SgsShopIncomeRequest;
import com.sgzn.sgpay.model.request.util.PageQueryParam;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.model.response.result.SgsShopIncomePreInfoResult;
import com.sgzn.sgpay.model.response.result.SgsShopIncomeResult;
import com.sgzn.sgpay.service.SgsShopIncomePreInfoService;
import com.sgzn.sgpay.service.SgsShopIncomeService;
import com.sgzn.sgpay.util.ArgsUtils;
import com.sgzn.sgpay.web.controller.base.MerchantBaseController;
import com.sgzn.sgpay.web.handle.AssembleCommonHandle;
import com.sgzn.sgpay.web.request.Impl.ShopIncomePreInfoRequest;
import com.sgzn.sgpay.web.request.Impl.ShopIncomeRequest;
import com.sgzn.sgpay.web.request.Impl.ShopQueryByIdRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 门店经营统计分析控制器
 * @author huangsenguang
 * @version Id: ShopStatisticsController.java, v 0.1 2018/6/3 16:53 huangsenguang Exp $$
 */
@Slf4j
@Controller
@RequestMapping("/merchant/statistics")
public class ShopStatisticsController extends MerchantBaseController {
    @Autowired
    private SgsShopIncomeService sgsShopIncomeService;

    @Autowired
    private SgsShopIncomePreInfoService sgsShopIncomePreInfoService;

    @Resource
    private AssembleCommonHandle assembleCommonHandle;

    /**
     * 根据门店id 查询门店管理的详细信息
     * @param request
     * @return
     */
    @RequestMapping("shopShowDetail")
    @ResponseBody
    public SgsResultResponse<SgsShopIncomeResult> queryShopInfo(ShopQueryByIdRequest request) {
        super.setMchInfo(request);
        return this.sgsShopIncomeService.queryInfo(assembleQueryRequest(request));
    }

    /**
     * 门店收益前查询展示信息
     * @param request
     * @return
     */
    @RequestMapping("shopIncomePreInfo")
    @ResponseBody
    public SgsResultResponse<SgsShopIncomePreInfoResult> queryIncome(ShopIncomePreInfoRequest request) {
        super.setMchInfo(request);
        return this.sgsShopIncomePreInfoService.queryInfo(assembleRequest(request));
    }

    /**
     * 门店经营分析
     * @param request
     * @return
     */
    @RequestMapping("shopIncome")
    @ResponseBody
    public SgsResultResponse<SgsShopIncomeResult> queryIncome(ShopIncomeRequest request) throws ParseException {
        super.setMchInfo(request);
        return this.sgsShopIncomeService.queryInfo(assembleRequest(request));
    }

    private SgsShopIncomeRequest assembleQueryRequest(ShopQueryByIdRequest request) {
        SgsShopIncomeRequest sgsShopQueryRequest = new SgsShopIncomeRequest();
        this.assembleCommonHandle.assemble(request, sgsShopQueryRequest);
        List<String> shopIdList = new ArrayList<>();
        shopIdList.add(request.getShopId());
        sgsShopQueryRequest.setShopIdList(shopIdList);
        PageQueryParam pageQueryParam = new PageQueryParam();
        pageQueryParam.setPageSize(1);
        pageQueryParam.setCurrentPage(1);
        sgsShopQueryRequest.setPageQueryParam(pageQueryParam);
        return sgsShopQueryRequest;
    }

    /**
     * 组装服务请求
     * @param request
     * @return
     */
    private SgsShopIncomePreInfoRequest assembleRequest(ShopIncomePreInfoRequest request) {
        SgsShopIncomePreInfoRequest sgsShopIncomePreInfoRequest = new SgsShopIncomePreInfoRequest();
        this.assembleCommonHandle.assemble(request, sgsShopIncomePreInfoRequest);
        return sgsShopIncomePreInfoRequest;
    }

    /**
     * 组装服务请求
     * @param request
     * @return
     */
    private SgsShopIncomeRequest assembleRequest(ShopIncomeRequest request) throws ParseException {
        SgsShopIncomeRequest sgsShopIncomeRequest = new SgsShopIncomeRequest();
        this.assembleCommonHandle.assemble(request, sgsShopIncomeRequest);
        if (StringUtils.isNotBlank(request.getShopIdList())) {
            String[] shopIds = request.getShopIdList().split(",");
            sgsShopIncomeRequest.setShopIdList(Arrays.asList(shopIds));
        }
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        sgsShopIncomeRequest.setBeginTime(simpleDateFormat.parse(request.getBeginTime()));
        if (StringUtils.isNotBlank(request.getEndTime())){
            Date endTime = simpleDateFormat.parse(request.getEndTime());
            endTime.setTime(endTime.getTime()+ 86399999);
            sgsShopIncomeRequest.setEndTime(endTime);
        } else {
            sgsShopIncomeRequest.setEndTime(new Date());
        }
        ArgsUtils.check(sgsShopIncomeRequest.getBeginTime().before(sgsShopIncomeRequest.getEndTime()),
                SgsResponseCode.C10018.getResponseCode(),
                SgsResponseCode.C10018.getResponseDesc());
        PageQueryParam pageQueryParam = new PageQueryParam();
        pageQueryParam.setPageSize(Integer.parseInt(request.getPageSize()));
        pageQueryParam.setCurrentPage(Integer.valueOf(request.getPageNum()));
        sgsShopIncomeRequest.setPageQueryParam(pageQueryParam);
        return sgsShopIncomeRequest;
    }

}

